<template>
  <div class="carousel-container">
    <div class="carousel-button">
      <el-button type="primary" @click="toAddPage">
        <i class="fa fa-plus"></i> 添加轮播图
      </el-button>
    </div>
    <el-skeleton class="carousel-row" :loading="loading" animated>
      <template slot="template">
        <div class="carousel-item">
          <div class="carousel-media">
            <el-skeleton-item variant="image" style="width: 100%; height: 100%;" />
          </div>
          <div class="carousel-describe">
            <p class="title">
              <el-skeleton-item variant="text" style="width: 100%;" />
            </p>
            <p class="subheading">
              <el-skeleton-item variant="text" style="width: 90%;" />
            </p>
            <p class="describe">
              <el-skeleton-item variant="text" style="width: 95%;" />
            </p>
            <div class="carousel-eidt">
              <el-skeleton-item variant="text" style="width: 10%;margin-right: 10px;" />
              <el-skeleton-item variant="text" style="width: 10%;" />
            </div>
          </div>
        </div>
      </template>
      <template>
        <div class="carousel-item" :id="item.id" v-for="(item, index) in carouselList" :key="item.id">
          <div class="carousel-media">
            <img v-if="item.mediaType == 0" :src="item.host + item.url" />
            <video v-else-if="item.mediaType == 1" muted autoplay loop :src="item.host + item.url"></video>
          </div>
          <div class="carousel-describe">
            <p class="title">
              <i v-if="item.mediaType == 0" class="fa fa-photo"></i>
              <i v-else-if="item.mediaType == 1" class="fa fa-file-video-o"></i>
              {{ item.title }}
            </p>
            <p class="subheading">{{ item.subtitle }}</p>
            <p class="describe">
              {{ item.description }}
            </p>
            <div class="carousel-eidt">
              <el-button type="text" @click="toEditPage(item)"><i class="fa fa-edit"></i> 编辑</el-button>
              <el-button class="text-danger" type="text" @click="deleteCarousel(item.id)"><i class="fa fa-trash"></i>
                删除</el-button>
            </div>
          </div>
        </div>
      </template>
    </el-skeleton>
  </div>
</template>

<script>
import * as carouselApi from '@/api/admin/carouselApi';

export default {
  data () {
    return {
      carouselList: [],
      loading: true,
    }
  },
  methods: {
    toAddPage () {
      this.$router.push('/management/home/CarouselForm')
    },
    getCarouselList () {
      var self = this;
      self.loading = true;
      carouselApi.getAllCarousels()
        .then(res => {
          if (res.code == 200) {
            self.carouselList = res.data;
          }
          else if (res.code == 500) {
            self.$message.error('接口发生错误');
          }
          self.loading = false;
        })
        .catch(err => {
          self.$message.error('添加轮播图接口发生错误');
          self.loading = false;
        })
    },
    toEditPage (item) {
      var self = this;
      self.$router.push({
        path: '/management/home/CarouselForm',
        query: {
          id: item.id,
        }
      })
    },
    deleteCarousel (id) {
      var self = this;
      this.$confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        carouselApi.deleteCarousel(id)
          .then(res => {
            if (res.code == 200) {
              self.$message({
                message: '删除成功',
                type: 'success'
              });
              document.getElementById(id).style.display = 'none';
            } else if (res.code == 500) {
              self.$message.error('接口发生错误');
            }
          })
          .catch(err => {
            self.$message.error('接口发生错误');
          });
      });
    }
  },
  mounted () {
    var self = this;
    self.getCarouselList()
  }
}
</script>

<style scoped>
.carousel-button {
  padding: 5px;
}

.carousel-row {
  display: flex;
  flex-flow: wrap;
}

.carousel-item {
  margin: 5px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #E4E7ED;
  width: calc(1920px / 5);
}

.carousel-media {
  background: black;
  height: calc(900px / 5);
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

.carousel-media img {
  width: 100%;
  height: 100%;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

.carousel-media video {
  width: 100%;
  height: 100%;
  object-fit: fill;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

.carousel-describe {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.carousel-card {
  padding: 0;
  margin: 8px;
}

.carousel-describe .title {
  font-size: 16px;
  margin: 5px 0px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.carousel-describe .subheading {
  color: #606266;
  font-size: 14px;
  margin: 5px 0px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.carousel-describe .describe {
  color: #909399;
  font-size: 12px;
  margin: 5px 0px;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sort-icon {
  margin: 0px 10px;
}

.sort-icon i {
  font-size: 24px;
}

.carousel-eidt {
  display: flex;
  align-items: center;
}

.el-button+.el-button {
  margin: 0px;
}

.el-button+.el-button {
  margin: 0px 8px;
}
</style>